package com.example;

import com.alibaba.druid.pool.DruidDataSource;
import com.example.dao.UserDao;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Application_10 {
    public static void main(String[] args) throws Exception {
        int id=1;
        String status="睡觉";

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        DruidDataSource dataSource = (DruidDataSource) context.getBean("dataSource");
        UserDao userDao= (UserDao) context.getBean("userDao");
        // 1.导入驱动jar包
        // 2.加载驱动
        Class.forName(dataSource.getDriverClassName());
        // 3.获取数据库连接对象
        Connection connection = DriverManager.getConnection(dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword());
        // 4.定义sql语句
        // 5.获取执行sql语句的对象
        Statement statement = connection.createStatement();
        // 6.执行sql并接收返回的对象
        statement.executeUpdate(userDao.updateUserStatusById(id,status));
        ResultSet resultSet = statement.executeQuery(userDao.selectUserById(id));
        // 7.处理结果
        while (resultSet.next()) {
            System.out.println("User(" + resultSet.getInt("uid") + ',' +
                    resultSet.getString("name") + ',' +
                    resultSet.getString("gender") + ',' +
                    resultSet.getInt("age") + ',' +
                    resultSet.getInt("parent_id") + ','+
                    resultSet.getString("status")+")");
        }
        // 8.关闭连接，释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}